/**
 * @file DB0.h
 * @author your name (you@domain.com)
 * @brief 第0个数据块
 * @version 0.1
 * @date 2025-04-11
 *
 * @copyright Copyright (c) 2025
 *
 */
#ifndef DB0_H
#define DB0_H
#include "stm32f4xx_hal.h" //该头文件必须包含，不然会#include "stm32f4xx_hal_gpio.h"报错！！！！
#include "sysconf.h"
#include "usertempconf.h"
#include "util.h"
#include "xmzTypeDefine.h"
#include <stdbool.h>
#include <stddef.h>
/* 设置结构体对齐方式为1字节 */

typedef struct {
  Bool          I[DIQM_NUM_LEN][DIQM_NUM_PIN]; //!< 128 byte
  Bool          Q[DIQM_NUM_LEN][DIQM_NUM_PIN];
  Bool          Q_W[DIQM_NUM_LEN][DIQM_NUM_PIN]; //!< 128 byte
  Bool          M[DIQM_NUM_LEN][DIQM_NUM_PIN];   //!< 128 byte
  int16_t       AI[AI_NUM_LEN];                  //!< 200 byte
  uint32_t      HSC[HSC_NUM];                    //!< 6个高速计数_24byte
  DTL           dateTime;                        //!< 读_12byte
  DTL           dateTime_w;                      //!< 写_12byte
  volatile Time sys_run_time;                    //!< 运行时间_4byte
  uint16_t      sys_over_count;                  //!< 溢出次数_2byte
  uint16_t      sys_error_code;                  //!< 错误码_2byte
  Bool          sys_set_time;                    //!< 设置时间_0.1
} DB0_s;

// 将系统掉电保持预设为8K,其中I,Q,M不保持
#define DB0_ZIP_SIZE 0x1000
#define DB0_SIZE sizeof(DB0_s)
STATIC_ASSERT(DB0_SIZE <= 0x2000, CUSTOM_CONF_TOO_LARGE_4K);
extern DB0_s*       g_DB0_t; /* DB0数据块结构体实例 */
extern Clock_Tick_S g_clock_tick;
/**
 * @brief 初始化DB0
 *
 */
void init_DB0(void);
void IN_pack_to_buffer(void);
void IN_unpack_from_buffer(void);

void OUT_pack_to_buffer(void);
void OUT_unpack_from_buffer(void);

void MEM_pack_to_buffer(void);
void MEM_unpack_from_buffer(void);

void DB0_pack_to_buffer(uint8_t* buffer, size_t buffer_size);
void DB0_unpack_from_buffer(uint8_t* buffer, size_t buffer_size);

#endif
